﻿
var userName = null;
var phone = null;
var email = null;
var userPassword = null;
var ruserPassword = null;
var addBtn = null;
var backBtn = null;


$(function(){
	userName = $("#userName");
	phone = $("#phone");
	email = $("#email");
	userPassword = $("#userPassword");
	ruserPassword = $("#ruserPassword");
	addBtn = $("#add");
	backBtn = $("#back");
	//初始化的时候，要把所有的提示信息变为：* 以提示必填项，更灵活，不要写在页面上
	userName.next().html("*");
	userPassword.next().html("*");
	ruserPassword.next().html("*");

	/*
	 * 验证
	 * 失焦\获焦
	 * jquery的方法传递
	 */
	
	userName.bind("focus",function(){
		validateTip(userName.next(),{"color":"#666666"},"* 用户名长度必须是大于1小于10的字符",false);
	}).bind("blur",function(){
		if(userName.val() != null && userName.val().length > 1
			&& userName.val().length < 10){
			validateTip(userName.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(userName.next(),{"color":"red"},imgNo+" 用户名输入的不符合规范，请重新输入",false);
		}
	});
	userPassword.bind("focus",function(){
		validateTip(userPassword.next(),{"color":"#666666"},"* 密码长度必须是大于6小于20",false);
	}).bind("blur",function(){
		if(userPassword.val() != null && userPassword.val().length > 6
				&& userPassword.val().length < 20 ){
			validateTip(userPassword.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(userPassword.next(),{"color":"red"},imgNo + " 密码输入不符合规范，请重新输入",false);
		}
	});
	
	ruserPassword.bind("focus",function(){
		validateTip(ruserPassword.next(),{"color":"#666666"},"* 请输入与上面一只的密码",false);
	}).bind("blur",function(){
		if(ruserPassword.val() != null && ruserPassword.val().length > 6
				&& ruserPassword.val().length < 20 && userPassword.val() == ruserPassword.val()){
			validateTip(ruserPassword.next(),{"color":"green"},imgYes,true);
		}else{
			validateTip(ruserPassword.next(),{"color":"red"},imgNo + " 两次密码输入不一致，请重新输入",false);
		}
	});
	addBtn.bind("click",function(event){
		event.preventDefault(); // 阻止默认提交行为
		if(userName.attr("validateStatus") != "true"){
			userName.blur();
		}else if(userPassword.attr("validateStatus") != "true"){
			userPassword.blur();
		}else if(ruserPassword.attr("validateStatus") != "true"){
			ruserPassword.blur();
		}else{
			let admindata = {
				name: userName.val(),
				phone: phone.val(),
				email: email.val()
			}
			$.ajax({
				url:path+"/gymjsp/admin.do",
				type:"GET",
				data:{method:"validAdmin",admin: JSON.stringify(admindata) },
				dataType:"json",
				success:function (data){
					console.log(data);
					if (data.name <= 0 && data.phone <= 0 && data.email <= 0){
						if(confirm("是否确认提交数据")){
							$("#userForm").submit();
						}
					}else {
						if (data.name > 0){
							window.alert("用户名已存在！");
						}
						if (data.phone > 0){
							window.alert("手机号已存在！");
						}
						if (data.email > 0){
							window.alert("邮箱已存在！")
						}
						return;
					}
				}
			})
		}
	});
	
	backBtn.on("click",function(){
		if(referer != undefined 
			&& null != referer 
			&& "" != referer
			&& "null" != referer
			&& referer.length > 4){
		 window.location.href = referer;
		}else{
			history.back(-1);
		}
	});
});